package com.baidu.fenfayun.demo.home;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.qmuiteam.qmui.util.QMUIResHelper;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.baidu.fenfayun.demo.base.BaseFragment;

import android.content.*;
import com.baidu.yun.gameline.R;
import java.util.HashMap;

import android.app.*;
import butterknife.BindView;
import butterknife.ButterKnife;
import android.widget.FrameLayout;

import kotlin.reflect.*;
/**
*/

public class HomeFragment extends BaseFragment {
   private final static String TAG = HomeFragment.class.getSimpleName();

   @BindView(R.id.pager) ViewPager mViewPager;
   @BindView(R.id.tabs) QMUITabSegment mTabSegment;
   private HashMap<Pager, FrameLayout> mPages;
   private PagerAdapter mPagerAdapter = new PagerAdapter() {

      private int mChildCount = 0;

      @Override
      public boolean isViewFromObject(View view, Object object) {
         return view == object;
      }

      @Override
      public int getCount() {
         return mPages.size();
      }

      @Override
      public Object instantiateItem(final ViewGroup container, int position) {
         FrameLayout page = mPages.get(Pager.getPagerFromPositon(position));
         ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
         if(container != null)container.addView(page, params);
         return page;
      }

      @Override
      public void destroyItem(ViewGroup container, int position, Object object) {
         if(container != null)container.removeView((View) object);
      }

      @Override
      public int getItemPosition(Object object) {
         if (mChildCount == 0) {
            return POSITION_NONE;
         }
         return super.getItemPosition(object);
      }

      @Override
      public void notifyDataSetChanged() {
         mChildCount = getCount();
         super.notifyDataSetChanged();
      }
   };


   @Override
   protected View onCreateView() {
      FrameLayout layout = (FrameLayout) LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home, null);
      ButterKnife.bind(this, layout);
      initTabs();
      initPagers();
      return layout;
   }

   private void initTabs() {
      int normalColor = QMUIResHelper.getAttrColor(getActivity(), R.attr.qmui_config_color_gray_6);
      int selectColor = QMUIResHelper.getAttrColor(getActivity(), R.attr.qmui_config_color_blue);
      mTabSegment.setDefaultNormalColor(normalColor);
      mTabSegment.setDefaultSelectedColor(selectColor);
      QMUITabSegment.Tab tweet = new QMUITabSegment.Tab(
            ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_lab),
            ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_lab_selected),
        "动弹", false
        );
      QMUITabSegment.Tab my = new QMUITabSegment.Tab(
            ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_lab),
            ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_lab_selected),
            "我的", false
            );
      QMUITabSegment.Tab lab = new QMUITabSegment.Tab(
            ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_lab),
            ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_lab_selected),
            "实验室", false
            );
      mTabSegment
         .addTab(tweet)
         .addTab(lab)
         .addTab(my);
   }

   private void initPagers() {

      HomeController.HomeControlListener listener = new HomeController.HomeControlListener() {
         @Override
         public void startFragment(BaseFragment fragment) {
            HomeFragment.this.startFragment(fragment);
         }
      };
      LayoutController.LayoutControlListener listener2 = new LayoutController.LayoutControlListener() {
         @Override
         public void startFragment(BaseFragment fragment) {
            HomeFragment.this.startFragment(fragment);
         }
         @Override
         public BaseFragment getBase() {

            return HomeFragment.this;
         }
      };

      mPages = new HashMap<Pager,FrameLayout>();

      LayoutController homeComponentsController = new TweetController(getActivity());
      homeComponentsController.setLayoutControlListener(listener2);
      mPages.put(Pager.TWEET, homeComponentsController);

      HomeController homeLabController = new HomeLabController(getActivity());
      homeLabController.setHomeControlListener(listener);
      mPages.put(Pager.LAB, homeLabController);

      LayoutController homeUtilController = new HomeMyController(getActivity());
      homeUtilController.setLayoutControlListener(listener2);
      mPages.put(Pager.MY, homeUtilController);


      mViewPager.setAdapter(mPagerAdapter);
      mTabSegment.setupWithViewPager(mViewPager, false);
   }

   enum Pager {
      TWEET, MY, LAB;

      public static Pager getPagerFromPositon(int position) {
         switch (position) {
            case 2:
               return MY;
            case 1:
               return LAB;
            case 0:
               return TWEET;
            default:
               return TWEET;
         }
      }
   }
}
